package com.bc.trade.channel.suning.util;

import java.nio.charset.Charset;

public class CoderBase {
	protected static final int OX0F = 15;
	protected static final int OXF0 = 240;
	protected static final int OX7F = 127;
	protected static final int OX80 = 128;
	protected static final int OXFF = 255;
	protected static final int BASE_2 = 2;
	protected static final int BASE_3 = 3;
	protected static final int BASE_4 = 4;
	protected static final int BASE_5 = 5;
	protected static final int BASE_6 = 6;
	protected static final int BASE_7 = 7;
	protected static final int BASE_8 = 8;
	protected static final int BASE_9 = 9;
	protected static final int BASE_10 = 10;
	protected static final int BASE_14 = 14;
	protected static final int BASE_15 = 15;
	protected static final int BASE_16 = 16;
	protected static final int BASE_19 = 19;
	protected static final int BASE_20 = 20;
	protected static final int BASE_24 = 24;
	protected static final int BASE_30 = 30;
	protected static final int BASE_32 = 32;
	protected static final int BASE_39 = 39;
	protected static final int BASE_40 = 40;
	protected static final int BASE_48 = 48;
	protected static final int BASE_55 = 55;
	protected static final int BASE_56 = 56;
	protected static final int BASE_59 = 59;
	protected static final int BASE_60 = 60;
	protected static final int BASE_63 = 63;
	protected static final int BASE_64 = 64;
	protected static final int BASE_79 = 79;
	protected static final int BASE_80 = 80;
	protected static final int BASE_127 = 127;
	protected static final int BASE_128 = 128;
	protected static final int BASE_255 = 255;
	static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
			'F'};
	private static final int NUM_8 = 8;

	public static int hexCharToInt(char c) {
		return CoderBase.hexCharToByte(c);
	}

	public static byte hexCharToByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(Character.toUpperCase(c));
		return b;
	}

	public static String baseIntToHex(int c) {
		char s = "0123456789ABCDEF".charAt(c);
		return String.valueOf(s);
	}

	public static String intToHex(int n) {
		StringBuilder sb = new StringBuilder();
		if (n / 16 == 0) {
			return CoderBase.baseIntToHex(n);
		}
		String t = CoderBase.intToHex(n / 16);
		int nn = n % 16;
		sb.append(t).append(CoderBase.baseIntToHex(nn));
		return sb.toString();
	}

	public static byte[] hexStringToBytes(String hex) {
		return CoderBase.hexStrToBytesByBase2(hex);
	}

	private static byte[] hexStrToBytesByBase2(String hex) {
		String hexTemp = hex.toUpperCase();
		int len = hexTemp.length() / 2;
		byte[] result = new byte[len];
		char[] achar = hexTemp.toCharArray();
		for (int i = 0; i < len; ++i) {
			int pos = i * 2;
			result[i] = (byte) (CoderBase.hexCharToByte(achar[pos]) << 4 | CoderBase.hexCharToByte(achar[pos + 1]));
		}
		return result;
	}

	public static String bytesToHexString(byte[] bArray) {
		return CoderBase.bytesToHexStringByInteger1(bArray);
	}

	private static String bytesToHexStringByInteger1(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		for (int i = 0; i < bArray.length; ++i) {
			String sTemp = Integer.toHexString(255 & bArray[i]);
			if (sTemp.length() < 2) {
				sb.append(0);
			}
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	public static char[] bytesToHexChars(byte[] content) {
		char[] out = new char[content.length << 1];
		int j = 0;
		int maxLength = 240;
		int byteLength = 4;
		int lineLength = 15;
		for (int i = 0; i < content.length; ++i) {
			out[j++] = HEX[(240 & content[i]) >>> 4];
			out[j++] = HEX[15 & content[i]];
		}
		return out;
	}

	public static int[] hexStringToBinary(String source) {
		int len = source.length();
		int[] dest = new int[len * 4];
		char[] arr = source.toCharArray();
		for (int i = 0; i < len; ++i) {
			int t = 0;
			t = CoderBase.hexCharToInt(arr[i]);
			String[] str = Integer.toBinaryString(t).split("");
			int k = i * 4 + 4 - 1;
			for (int j = str.length - 1; j > 0; --j) {
				dest[k] = Integer.parseInt(str[j]);
				--k;
			}
		}
		return dest;
	}

	public static int[] bytesToBinary(byte[] source) {
		int[] dest = new int[source.length * 8];
		for (int i = 0; i < source.length; ++i) {
			String[] temp = Integer.toBinaryString(source[i]).split("");
			for (int k = 1; k < temp.length; ++k) {
				dest[8 * i + k + 8 - temp.length] = Integer.parseInt(temp[k]);
			}
		}
		return dest;
	}

	private static int getXY(int x, int y) {
		int result = x;
		if (y == 0) {
			result = 1;
		}
		for (int i = 2; i <= y; ++i) {
			result *= x;
		}
		return result;
	}

	public static String baseBinaryStrToHexString(String s) {
		int len = s.length();
		int result = 0;
		int k = 0;
		if (len > 4) {
			return null;
		}
		for (int i = len; i > 0; --i) {
			result += Integer.parseInt(s.substring(i - 1, i)) * CoderBase.getXY(2, k);
			++k;
		}
		return Integer.toHexString(result).toUpperCase();
	}

	public static String binaryStrToHexString(String s) {
		StringBuffer info = new StringBuffer("");
		int ii = 0;
		int len = s.length();
		String temp = s;
		if (len % 4 != 0) {
			while (ii++ < 4 - len % 4) {
				temp = "0" + temp;
			}
		}
		for (int i = 0; i < len / 4; ++i) {
			info.append(CoderBase.baseBinaryStrToHexString(temp.substring(i * 4, i * 4 + 4)));
		}
		return info.toString();
	}

	public static String bcdToString(byte[] bytes) {
		char[] temp = new char[bytes.length * 2];
		for (int i = 0; i < bytes.length; ++i) {
			char val = (char) ((bytes[i] & 240) >> 4 & 15);
			temp[i * 2] = (char) (val > '\t' ? val + 65 - 10 : val + 48);
			val = (char) (bytes[i] & 15);
			temp[i * 2 + 1] = (char) (val > '\t' ? val + 65 - 10 : val + 48);
		}
		return new String(temp);
	}

	public static byte[] stringToBcd(String iasc) {
		String asc = iasc;
		int hex = 10;
		int index = 4;
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}
		if (len >= 2) {
			len /= 2;
		}
		byte[] bbt = new byte[len];
		byte[] abt = asc.getBytes(Charset.defaultCharset());
		for (int p = 0; p < asc.length() / 2; ++p) {
			byte b;
			int j = abt[2 * p] >= 48 && abt[2 * p] <= 57
					? abt[2 * p] - 48
					: (abt[2 * p] >= 97 && abt[2 * p] <= 122 ? abt[2 * p] - 97 + 10 : abt[2 * p] - 65 + 10);
			int k = abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57
					? abt[2 * p + 1] - 48
					: (abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122
							? abt[2 * p + 1] - 97 + 10
							: abt[2 * p + 1] - 65 + 10);
			int a = (j << 4) + k;
			bbt[p] = b = (byte) a;
		}
		return bbt;
	}
}